<?php
/**
 * Used the modify the payment cost if it the payment is Klarna invoice, also used to validate
 * Klarnas payment options. To make sure that the required fields are being filled out.
 *
 * @author Klarna
 */
class klarnaInstall extends oxI18n {

	private $status = array('createtables' => false,
							'insertpayments' => false,
							'addusergroups' => false,
							'addshippingtypes' => false,
							'addcountries' => false,
							'amoduleinoxconfig' => false,
						);

	protected $_sClassName = 'klarnainstall';

	public function __construct() {
		parent::__construct();
		$this->init('klarnainstall');
	}

	private $lang = null;
	private $kutil = null;
	private $steps = null;
	private $oDB = null;
	private $shopId = null;

	/**
	 * These arrays shall contain all the classes that is being extended. And the extension path.
	 * These arrays will later on be checked and if it is in there, in the system or not,
	 * another proceedure in the installation shall be taken.
	 */
	protected $classesToAdd = array('klarna/core/klarnapaymentgateway' => 'oxpaymentgateway',
									'klarna/core/klarnapayment' => 'oxpayment',
									'klarna/core/klarnaarticle' => 'oxarticle',
									'klarna/core/klarnaemail' => 'oxemail',
									'klarna/views/klarna_payment' => 'payment',
									'klarna/views/klarna_thankyou' => 'thankyou',
									'klarna/views/klarna_order' => 'order',
									'klarna/views/klarna_details' => 'details',
									'klarna/admin/klarna_order_overview' => 'order_overview',
									'klarna/admin/klarna_order_main' => 'order_main',
									);

	protected $classesToAdd2 = array(
									'oxpaymentgateway' => 'klarna/core/klarnapaymentgateway',
									'oxpayment' => 'klarna/core/klarnapayment',
									'oxarticle' => 'klarna/core/klarnaarticle',
									'oxemail' => 'klarna/core/klarnaemail',
									'payment' => 'klarna/views/klarna_payment',
									'thankyou' => 'klarna/views/klarna_thankyou',
									'order' => 'klarna/views/klarna_order',
									'details' => 'klarna/views/klarna_details',
									'order_overview' => 'klarna/admin/klarna_order_overview',
									'order_main' => 'klarna/admin/klarna_order_main',
									);
	protected $currenciesToAdd = array(
										'SEK' => 0,
										'NOK' => 0,
										'DKK' => 0,
										);

	/**
	 * Creates the DB tables needed. The klarnainstall table is created if needed in
	 * klarnautil, where the the instance of this object is fetched.
	 *
	 * Also fills the klarnaonfig table with default entries.
	 *
	 * @return bool true if successfull or already done and false if failure.
	 */
	public function createTables() {
		if($this->klarnainstall__createtables->value) {
			return true;
		}

		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}
		if(!$this->oDB) {
			$this->oDB = oxDb::getDb(true);
		}
		if(!$this->shopId) {
			$myConfig = $this->getConfig();
			$this->shopId = $myConfig->getShopId();
		}

		$sql = "CREATE TABLE IF NOT EXISTS klarnapclasses (
					OXID INT(11) PRIMARY KEY AUTO_INCREMENT NOT NULL,
					OXSHOPID CHAR(32) DEFAULT NULL,
					PCLASSID INT(11) DEFAULT NULL,
					DESCRIPTION TEXT DEFAULT NULL,
					MONTHS INT(11) DEFAULT NULL,
					STARTFEE INT(11) DEFAULT NULL,
					HANDLINGFEE INT(11) DEFAULT NULL,
					INTEREST FLOAT DEFAULT NULL,
					MINAMOUNT INT(11) DEFAULT NULL,
					CURRENCY VARCHAR(255) DEFAULT NULL,
					DISABLED TINYINT(1) NOT NULL DEFAULT 0,
					TYPE TINYINT(4) NOT NULL DEFAULT -1,
					COUNTRY INT(11) NOT NULL DEFAULT 0
				);";

		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$sql = "CREATE TABLE IF NOT EXISTS klarnaconfig (
					OXID CHAR(32) PRIMARY KEY,
					EID INT,
					SECRET VARCHAR(25),
					CURRVERSION VARCHAR(40),
					TESTMODE TINYINT DEFAULT 0,
					INVOICEFEEDE DOUBLE,
					INVOICEFEESE DOUBLE,
					INVOICEFEENO DOUBLE,
					INVOICEFEEFI DOUBLE,
					INVOICEFEEDK DOUBLE,
					INVOICEFEENL DOUBLE,
					SELLTO TEXT,
					YSALARYDE DOUBLE,
					YSALARYSE DOUBLE,
					YSALARYNO DOUBLE,
					YSALARYFI DOUBLE,
					YSALARYDK DOUBLE,
					YSALARYNL DOUBLE,
					DEFAULTCOUNTRY VARCHAR(4),
					VENDORCOUNTRY VARCHAR(4)
		);";
		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$sellTo = '';
		foreach($this->kutil->getDefaultCountries() as $country => $value) {
			$sellTo .= '1|'.$country.'__@@';
		}

		$sql = "INSERT IGNORE INTO klarnaconfig (OXID, SELLTO, YSALARYDE, YSALARYSE, YSALARYNO,
				YSALARYFI, YSALARYDK, YSALARYNL)
				VALUES ('$this->shopId', '$sellTo', -1, -1, -1, -1, 1, -1);";
		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$this->klarnainstall__createtables->rawValue = 1;
		$this->save();

		return true;
	}

	/**
	 * Inserts the payment options into the DB table oxpamyents. If klarnas payments are
	 * already present it will not overwrite them.
	 *
	 * @return bool true if successfull or already done and false if failure.
	 */
	public function insertPayments() {
		if($this->klarnainstall__insertpayments->value) {
			return true;
		}

		if(!$this->oDB) {
			$this->oDB = oxDb::getDb(true);
		}

		//This is divided up like this in case there already is a column namned e.g. oxdesc_4 so that it aint
		//destroying for the other column adds.
		$sql = "ALTER TABLE oxpayments ADD (OXDESC_4 VARCHAR(128), OXVALDESC_4 TEXT);"; $this->oDB->execute($sql);
		$sql = "ALTER TABLE oxpayments ADD (OXDESC_5 VARCHAR(128), OXVALDESC_5 TEXT);"; $this->oDB->execute($sql);
		$sql = "ALTER TABLE oxpayments ADD (OXDESC_6 VARCHAR(128), OXVALDESC_6 TEXT);"; $this->oDB->execute($sql);
		$sql = "ALTER TABLE oxpayments ADD (OXDESC_7 VARCHAR(128), OXVALDESC_7 TEXT);"; $this->oDB->execute($sql);
		$sql = "ALTER TABLE oxpayments ADD (OXDESC_8 VARCHAR(128), OXVALDESC_8 TEXT);"; $this->oDB->execute($sql);
		$sql = "ALTER TABLE oxpayments ADD (OXDESC_9 VARCHAR(128), OXVALDESC_9 TEXT);"; $this->oDB->execute($sql);
		$sql = "ALTER TABLE oxpayments ADD (OXDESC_10 VARCHAR(128), OXVALDESC_10 TEXT);"; $this->oDB->execute($sql);

		$tempName = 'Klarna invoice';
		$tempContent = 'kigender__@@kibirth__@@kifon__@@kimobfon__@@kiuser__@@kistreet__@@kistreetnr__@@kihouseextension__@@';
		$sql = "INSERT IGNORE INTO oxpayments (OXID, OXACTIVE, OXDESC, OXDESC_1, OXFROMAMOUNT, OXTOAMOUNT, OXVALDESC, OXVALDESC_1, OXCHECKED, OXLONGDESC, OXLONGDESC_1, OXSORT) VALUES
				('klarnainvoice', 1, 'Klarna invoice', 'Klarna invoice', 0, 1000000, '$tempContent', '$tempContent', 1, '', '', -100);";
		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$sql = "UPDATE oxpayments SET OXDESC_3 = '$tempName', OXVALDESC_3 = '$tempContent',
										OXDESC_4 = '$tempName', OXVALDESC_4 = '$tempContent',
										OXDESC_5 = '$tempName', OXVALDESC_5 = '$tempContent',
										OXDESC_6 = '$tempName', OXVALDESC_6 = '$tempContent',
										OXDESC_7 = '$tempName', OXVALDESC_7 = '$tempContent',
										OXDESC_8 = '$tempName', OXVALDESC_8 = '$tempContent',
										OXDESC_9 = '$tempName', OXVALDESC_9 = '$tempContent',
										OXDESC_10 = '$tempName', OXVALDESC_10 = '$tempContent'
										WHERE oxid = 'klarnainvoice';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}


		$tempName = 'Klarna part payment';
		$tempContent = 'kppgender__@@kppbirth__@@kppfon__@@kppmobfon__@@kppuser__@@kppstreet__@@kppstreetnr__@@kppysalary__@@kpppclassid__@@kpphouseextension__@@';
		$sql = "INSERT IGNORE INTO oxpayments (OXID, OXACTIVE, OXDESC, OXDESC_1, OXFROMAMOUNT, OXTOAMOUNT, OXVALDESC, OXVALDESC_1, OXCHECKED, OXLONGDESC, OXLONGDESC_1, OXSORT) VALUES
				('klarnapartpayment', 1, 'Klarna part payment', 'Klarna part payment', 0, 1000000, 'kppgender__@@kppbirth__@@kppfon__@@kppmobfon__@@kppuser__@@kppstreet__@@kppstreetnr__@@kppysalary__@@kpppclassid__@@kpphouseextension__@@', 'kppgender__@@kppbirth__@@kppfon__@@kppmobfon__@@kppuser__@@kppstreet__@@kppstreetnr__@@kppysalary__@@kpppclassid__@@kpphouseextension__@@', 1, '', '', -99);";
		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$sql = "UPDATE oxpayments SET OXDESC_3 = '$tempName', OXVALDESC_3 = '$tempContent',
										OXDESC_4 = '$tempName', OXVALDESC_4 = '$tempContent',
										OXDESC_5 = '$tempName', OXVALDESC_5 = '$tempContent',
										OXDESC_6 = '$tempName', OXVALDESC_6 = '$tempContent',
										OXDESC_7 = '$tempName', OXVALDESC_7 = '$tempContent',
										OXDESC_8 = '$tempName', OXVALDESC_8 = '$tempContent',
										OXDESC_9 = '$tempName', OXVALDESC_9 = '$tempContent',
										OXDESC_10 = '$tempName', OXVALDESC_10 = '$tempContent'
										WHERE oxid = 'klarnapartpayment';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$tempName = 'Klarna special campaign';
		$tempContent = 'kscgender__@@kscbirth__@@kscfon__@@kscmobfon__@@kscuser__@@kscstreet__@@kscstreetnr__@@kscysalary__@@kscpclassid__@@kschouseextension__@@';
		$sql = "INSERT IGNORE INTO oxpayments (OXID, OXACTIVE, OXDESC, OXDESC_1, OXFROMAMOUNT, OXTOAMOUNT, OXVALDESC, OXVALDESC_1, OXCHECKED, OXLONGDESC, OXLONGDESC_1, OXSORT) VALUES
				('klarnaspecialcampaign', 1, 'Klarna special campaign', 'Klarna special campaign', 0, 1000000, 'kscgender__@@kscbirth__@@kscfon__@@kscmobfon__@@kscuser__@@kscstreet__@@kscstreetnr__@@kscysalary__@@kscpclassid__@@kschouseextension__@@', 'kscgender__@@kscbirth__@@kscfon__@@kscmobfon__@@kscuser__@@kscstreet__@@kscstreetnr__@@kscysalary__@@kscpclassid__@@kschouseextension__@@', 1, '', '', -98);";
		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$sql = "UPDATE oxpayments SET OXDESC_3 = '$tempName', OXVALDESC_3 = '$tempContent',
										OXDESC_4 = '$tempName', OXVALDESC_4 = '$tempContent',
										OXDESC_5 = '$tempName', OXVALDESC_5 = '$tempContent',
										OXDESC_6 = '$tempName', OXVALDESC_6 = '$tempContent',
										OXDESC_7 = '$tempName', OXVALDESC_7 = '$tempContent',
										OXDESC_8 = '$tempName', OXVALDESC_8 = '$tempContent',
										OXDESC_9 = '$tempName', OXVALDESC_9 = '$tempContent',
										OXDESC_10 = '$tempName', OXVALDESC_10 = '$tempContent'
										WHERE oxid = 'klarnaspecialcampaign';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}


		$this->klarnainstall__insertpayments->rawValue = 1;
		$this->save();

		return true;
	}

	/**
	 * Deletes all old entries of usergroup choices stored in the oxobject2group table
	 * assigned to klarnas payments.
	 * Then refills the tables will all usergroups found in the oxgroups table.
	 *
	 * @return bool true if successfull or already done and false if failure.
	 */
	public function addUserGroups() {
		if($this->klarnainstall__addusergroups->value) {
			return true;
		}

		$oGroups = oxNew('oxlist');
		$oGroups->init('oxgroups');
		$oGroups->selectString('SELECT OXID from oxgroups;');

		if(!$this->oDB) {
			$this->oDB = oxDb::getDb(true);
		}
		if(!$this->shopId) {
			$myConfig = $this->getConfig();
			$this->shopId = $myConfig->getShopId();
		}
		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}

		$sql = "DELETE FROM oxobject2group WHERE OXOBJECTID = 'klarnainvoice';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}
		$sql = "DELETE FROM oxobject2group WHERE OXOBJECTID = 'klarnapartpayment';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}
		$sql = "DELETE FROM oxobject2group WHERE OXOBJECTID = 'klarnaspecialcampaign';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$iName = $this->kutil->getIName();
		$ppName = $this->kutil->getPPName();
		$scName = $this->kutil->getSCName();
		$sql = '';
		foreach($oGroups as $item) {
			$id = $item->oxgroups__oxid->value;
			$randOne = md5(uniqid());
			$randTwo = md5(uniqid());
			$randThree = md5(uniqid());

			$sql = "INSERT IGNORE INTO oxobject2group (OXID, OXSHOPID, OXOBJECTID, OXGROUPSID) VALUES ('$randOne', '$this->shopId', '$iName', '$id');";
			if(!$this->oDB->execute($sql)) {
				return false;
			}

			$sql = "INSERT IGNORE INTO oxobject2group (OXID, OXSHOPID, OXOBJECTID, OXGROUPSID) VALUES ('$randTwo', '$this->shopId', '$ppName', '$id');";
			if(!$this->oDB->execute($sql)) {
				return false;
			}

			$sql = "INSERT IGNORE INTO oxobject2group (OXID, OXSHOPID, OXOBJECTID, OXGROUPSID) VALUES ('$randThree', '$this->shopId', '$scName', '$id');";
			if(!$this->oDB->execute($sql)) {
				return false;
			}
		}

		$this->klarnainstall__addusergroups->rawValue = 1;
		$this->save();

		return true;
	}

	/**
	 * Deletes all old entries of oxdelset choices stored in the oxobject2payment table
	 * assigned to klarnas payments.
	 * Then refills the tables will all deliverysets found in the oxdeliveryset table.
	 *
	 * @return bool true if successfull or already done and false if failure.
	 */
	public function addShippingTypes() {
		if($this->klarnainstall__addshippingtypes->value) {
			return true;
		}

		$oGroups = oxNew( "oxlist" );
		$oGroups->init( 'oxdeliveryset' );
		$oGroups->selectString("SELECT OXID from oxdeliveryset;");

		if(!$this->oDB) {
			$this->oDB = oxDb::getDb(true);
		}
		if(!$this->shopId) {
			$myConfig = $this->getConfig();
			$this->shopId = $myConfig->getShopId();
		}
		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}

		$sql = "DELETE FROM oxobject2payment WHERE OXPAYMENTID = 'klarnainvoice' AND OXTYPE = 'oxdelset';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}
		$sql = "DELETE FROM oxobject2payment WHERE OXPAYMENTID = 'klarnapartpayment' AND OXTYPE = 'oxdelset';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}
		$sql = "DELETE FROM oxobject2payment WHERE OXPAYMENTID = 'klarnaspecialcampaign' AND OXTYPE = 'oxdelset';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$iName = $this->kutil->getIName();
		$ppName = $this->kutil->getPPName();
		$scName = $this->kutil->getSCName();
		$sql = '';
		foreach($oGroups as $item) {
			$id = $item->oxdeliveryset__oxid->value;
			$randOne = md5(uniqid());
			$randTwo = md5(uniqid());
			$randThree = md5(uniqid());

			$sql = "INSERT IGNORE INTO oxobject2payment (OXID, OXPAYMENTID, OXOBJECTID, OXTYPE) VALUES ('$randOne', '$iName', '$id', 'oxdelset');";
			if(!$this->oDB->execute($sql)) {
				return false;
			}

			$sql = "INSERT IGNORE INTO oxobject2payment (OXID, OXPAYMENTID, OXOBJECTID, OXTYPE) VALUES ('$randTwo', '$ppName', '$id', 'oxdelset');";
			if(!$this->oDB->execute($sql)) {
				return false;
			}

			$sql = "INSERT IGNORE INTO oxobject2payment (OXID, OXPAYMENTID, OXOBJECTID, OXTYPE) VALUES ('$randThree', '$scName', '$id', 'oxdelset');";
			if(!$this->oDB->execute($sql)) {
				return false;
			}
		}

		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$this->klarnainstall__addshippingtypes->rawValue = 1;
		$this->save();

		return true;
	}

	/**
	 * Deletes all old entries of oxcountry choices stored in the oxobject2payment table
	 * assigned to klarnas payments.
	 * Then refills the tables will all active countries found in the oxcountry table.
	 *
	 * @return bool true if successfull or already done and false if failure.
	 */
	public function addCountries() {
		if($this->klarnainstall__addcountries->value) {
			return true;
		}

		if(!$this->oDB) {
			$this->oDB = oxDb::getDb(true);
		}
		if(!$this->shopId) {
			$myConfig = $this->getConfig();
			$this->shopId = $myConfig->getShopId();
		}
		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}

		$countries = $this->kutil->getDefaultCountries();
		foreach ($countries as $key => $value) {
			if($key == 'NO') {
				$sql = "UPDATE oxcountry SET OXACTIVE = 1, OXVATSTATUS = 1 WHERE OXISOALPHA2 = '$key';";
			} else {
				$sql = "UPDATE oxcountry SET OXACTIVE = 1 WHERE OXISOALPHA2 = '$key';";
			}
			if(!$this->oDB->execute($sql)) {
				return false;
			}
		}

		$oGroups = oxNew( "oxlist" );
		$oGroups->init( 'oxcountry' );
		$oGroups->selectString("SELECT OXID from oxcountry WHERE OXACTIVE = 1;");

		$sql = "DELETE FROM oxobject2payment WHERE OXPAYMENTID = 'klarnainvoice' AND OXTYPE = 'oxcountry';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}
		$sql = "DELETE FROM oxobject2payment WHERE OXPAYMENTID = 'klarnapartpayment' AND OXTYPE = 'oxcountry';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}
		$sql = "DELETE FROM oxobject2payment WHERE OXPAYMENTID = 'klarnaspecialcampaign' AND OXTYPE = 'oxcountry';";
		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$iName = $this->kutil->getIName();
		$ppName = $this->kutil->getPPName();
		$scName = $this->kutil->getSCName();
		$sql = '';
		foreach($oGroups as $item) {
			$id = $item->oxcountry__oxid->value;
			$randOne = md5(uniqid());
			$randTwo = md5(uniqid());
			$randThree = md5(uniqid());

			$sql = "INSERT IGNORE INTO oxobject2payment (OXID, OXPAYMENTID, OXOBJECTID, OXTYPE) VALUES ('$randOne', '$iName', '$id', 'oxcountry');";
			if(!$this->oDB->execute($sql)) {
				return false;
			}

			$sql = "INSERT IGNORE INTO oxobject2payment (OXID, OXPAYMENTID, OXOBJECTID, OXTYPE) VALUES ('$randTwo', '$ppName', '$id', 'oxcountry');";
			if(!$this->oDB->execute($sql)) {
				return false;
			}

			$sql = "INSERT IGNORE INTO oxobject2payment (OXID, OXPAYMENTID, OXOBJECTID, OXTYPE) VALUES ('$randThree', '$scName', '$id', 'oxcountry');";
			if(!$this->oDB->execute($sql)) {
				return false;
			}
		}

		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$this->klarnainstall__addcountries->rawValue = 1;
		$this->save();

		return true;
	}

	/**
	 * Defines that our classes extends some of the already existing base files. And then
	 * saves it back to the DB.
	 *
	 * @return bool true if successfull or already done and false if failure.
	 */
	public function aModuleInOxConfig() {
		if($this->klarnainstall__amoduleinoxconfig->value) {
			return true;
		}

		if(!$this->shopId) {
			$myConfig = $this->getConfig();
			$this->shopId = $myConfig->getShopId();
		}

		$aModules = oxConfig::getInstance()->getConfigParam('aModules');

		/**
		 * This part takes care of cases when a class is already present in the modules
		 * extension list i.e. cases when a class is alerady being extended and also making sure
		 * that klarnas classes aint double extending the superclasses.
		 */
		if(!empty($aModules)) {
			$isExtensionAdded = false;
			foreach ($aModules as $sParentName => &$sModuleName) {
				if (in_array($sParentName, $this->classesToAdd)) {

					//Goes through already existing extensions, if our listed items are in it, then dont add it.
					//Otherwise, do add it.
					$extensions = preg_split('/[&]/', $sModuleName);
					foreach($extensions as $ext) {
						if($ext == $this->classesToAdd2[$sParentName]) {
							$isExtensionAdded = true;
							continue;
						}
					}
					//Adds the extension if the parentclass is already there, but not our extension.
					if(!$isExtensionAdded) {
						$sModuleName .= '&';
						$sModuleName .=	$this->classesToAdd2[$sParentName];
					}
					$isExtensionAdded = false;
				}
				//Removes the added extension, or the extension that is already added since before.
				unset($this->classesToAdd2[$sParentName]);
			}
		}
		//Adds all extension that isnt already added
		foreach($this->classesToAdd2 as $class => $path) {
			$aModules[$class] = $path;
		}

		$myConfig = $this->getConfig();
		$myConfig->saveShopConfVar( "aarr", "aModules", $aModules, $this->shopId );

		$this->klarnainstall__amoduleinoxconfig->rawValue = 1;
		$this->save();

		return true;
	}

	/**
	 * Checks how many default countries there is. If there is only 1, then it will set it
	 * as default in the configuration table, otherwise it will return false.
	 *
	 * @param string $country isoalpha2
	 *
	 * @return bool true if the DB update was done false otherwise.
	 */
	public function defaultCountry() {
		if($this->klarnainstall__defaultcountry->value) {
			return true;
		}

		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}

		if(count($this->kutil->getDefaultCountries()) != 1) {
			return false;
		}

		if(count($this->kutil->getDefaultCountries()) == 1) {
			foreach($this->kutil->getDefaultCountries() as $thecountry => $value) {
				$country = $thecountry;
			}
		}
		if(!$this->oDB) {
			$this->oDB = oxDb::getDb(true);
		}
		if(!$this->shopId) {
			$myConfig = $this->getConfig();
			$this->shopId = $myConfig->getShopId();
		}

		$kconfig = $this->kutil->getKlarnaConfig();

		$sql = "UPDATE klarnaconfig SET DEFAULTCOUNTRY = '$country' WHERE OXID = '$this->shopId';";

		if(!$this->oDB->execute($sql)) {
			return false;
		}

		$this->klarnainstall__defaultcountry->rawValue = 1;
		$this->save();

		return true;
	}

	/**
	 * Adds more currencies if they aint already present.
	 *
	 * @return bool true if successfull or already done and false if failure.
	 */
	public function aCurrencies() {
		if($this->klarnainstall__acurrencies->value) {
			return true;
		}

		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}

//		if(!$this->shopId) {
//			$myConfig = $this->getConfig();
//			$this->shopId = $myConfig->getShopId();
//		}
//
//		$aCurrencies = oxConfig::getInstance()->getConfigParam('aCurrencies');
//
//		/**
//		 * This part takes care of cases when a class is already present in the modules
//		 * extension list i.e. cases when a class is alerady being extended and also making sure
//		 * that klarnas classes aint double extending the superclasses.
//		 */
//		$xml = simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
//		$mainCurr = '';
//		if(!empty($aCurrencies)) {//$currenciesToAdd
//			foreach ($aCurrencies as $aCurrenciesName) {
//				$currencyInfo = preg_split('/[@]/', $aCurrenciesName);
//
//				//Begins at one, as it contains a space.
//				$mainCurr = (substr($currencyInfo[1], 1, 4) == '1.00') ? $currencyInfo[0] : $mainCurr;
//
//				if (in_array($currencyInfo[0], $this->currenciesToAdd)) {
//					unset($this->currenciesToAdd[$currencyInfo[0]]);
//					continue;
//				}
//			}
//		}
//
//		$rate = '1';
//		if($mainCurr == 'EUR') {
//		} else if($mainCurr != '') {
//			$inXMLDocument = false;
//
//			foreach($xml->Cube->Cube->Cube as $child) {
//				if($mainCurr == $child['currency']) {
//					$inXMLDocument = true;
//					$rate = $child['rate'];
//				}
//			}
//
//			if(!$inXMLDocument) {
//				//Nothing can be done, the currency aint in the xml document.
//				return $this->aCurrenciesFinish();
//			}
//
//			//For some reason the decimals doesnt work without substr on $rate.
//			$rate = 1/substr($rate, 0, 6);
//		} else {
//			//Nothing can be done, no main currency, the customer will have to set the rate by them selfs.
//			return $this->aCurrenciesFinish();
//		}
//
//		foreach($xml->Cube->Cube->Cube as $child) {
//			if(	($child['currency'] == 'SEK' && in_array($child['currency'], $this->currenciesToAdd)) ||
//				($child['currency'] == 'NOK' && in_array($child['currency'], $this->currenciesToAdd)) ||
//				($child['currency'] == 'DKK' && in_array($child['currency'], $this->currenciesToAdd))
//				) {
//
//				//For some reason the decimals doesnt work without substr on child['rate'].
//				$aCurrencies[] = $child['currency'] . '@ ' .
//							substr($rate*substr($child['rate'], 0, 6), 0, 6) . '@ .@  @ kr@ 2';
//			}
//		}
//
//		$myConfig = $this->getConfig();
//		$myConfig->saveShopConfVar( "arr", "aCurrencies", $aCurrencies, $this->shopId );

		$this->kutil->addNewCurrencies();
		$this->klarnainstall__acurrencies->rawValue = 1;
		$this->save();
		return true;
	}

	/**
	 * Checks if the installation process is done.
	 *
	 * @return bool returns true if all steps in the installation is done, otherwise false.
	 */
	public function isDone() {
		return $this->checkIfDone();
	}

	/**
	 * Was ment to do more definite things like removing installations files.
	 * Might be removed when creating the release version.
	 *
	 * @return bool returns true if all steps in the installation is done, otherwise false.
	 */
	protected function checkIfDone() {
		if(	$this->klarnainstall__done->value &&
			$this->klarnainstall__createtables->value &&
			$this->klarnainstall__insertpayments->value &&
			$this->klarnainstall__addusergroups->value &&
			$this->klarnainstall__addcountries->value &&
			$this->klarnainstall__addshippingtypes->value &&
			$this->klarnainstall__amoduleinoxconfig->value &&
			$this->klarnainstall__saveconfig->value) {

			return true;
		}

		return false;
	}

	/**
	 * Creates a unique ID. Uses uniqid() and md5 to create a unique id and a 32char long string.
	 *
	 * @return a unique 32char length id.
	 */
	public function getUniqueID() {
		return md5(uniqid());
	}
}